package com.k.kpp.configfile;


import com.k.kc.util.PropertyReader;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

/**
 * @author k
 */
public class PropertiesFileModifyServiceImpl {


    /**
     * properties 文件中 有中文必须以ascii码形式存在, 有中文修改会有问题
     * @param pathname pathname
     * @param backPathname backPathName
     * @param keyValue keyValues
     */
    public static void updateProperties(String pathname,String backPathname, Hashtable<String,String> keyValue){
        File file = new File(pathname);
        File backupFile = new File(backPathname);
        try {
            FileUtils.copyFile(file,backupFile);
            System.out.println("backup success!");
            PropertyReader.batchUpdateValue(pathname,keyValue);
            System.out.println("update success!");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        //String src = "C:\\test\\config\\strings_zh.properties";
        String src = "C:\\test\\config\\application.properties";
        String back = src+".bak";

        Hashtable hashtable = new Hashtable();
        hashtable.put("kpp.name","my kpp name");
        hashtable.put("server.name","my server name");
        updateProperties(src, back, hashtable);
    }
}
